<?php

/**
 * This is the model class for table "bln_user".
 *
 * The followings are the available columns in table 'bln_user':
 * @property integer $_id
 * @property string $username
 * @property string $password
 * @property string $salt
 * @property string $email
 * @property integer $defaultservice
 * @property string $profile
 */
class User extends CActiveRecord {
    public $WorkingTeams = array();
    public $ServedTeams = array();
    public $FriendTeams = array();
    public $Services = array();
    /**
     * Returns the static model of the specified AR class.
     * @return User the static model class
     */    
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'bln_user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password, salt, email, defaultservice', 'required'),
            array('defaultservice', 'numerical', 'integerOnly' => true),
            array('username, password, salt, email', 'length', 'max' => 128),
            array('profile', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('_id, username, email, defaultservice, profile', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            '_id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'salt' => 'Salt',
            'email' => 'Email',
            'defaultservice' => 'Defaultservice',
            'profile' => 'Profile',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('_id', $this->_id);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('defaultservice', $this->defaultservice);
        $criteria->compare('profile', $this->profile, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function validatePassword($password) {
        return $this->hashPassword($password, $this->salt) === $this->password;
    }

    public function hashPassword($password, $salt) {
        return md5($salt . $password);
    }

    protected function generateSalt() {
        return uniqid('bln_', true);
    }    

    public function getUserCircles($userId)
    {
         $rows = $this->db->createCommand()
                        ->select()
                        ->from($this->circleUserTable)
                        ->where('_id=:userid', array(':userid' => $userId))
                        ->queryAll();
        $circles = array();
        foreach ($rows as $row) {
            $circle = new Circle($row['circleid']);
            $circles[$circle->getCircleId()]=$circle;

            //find all user's circle's all parent circles
            while ($circle=$circle->getParentCircle())
            {
                $circles[$circle->getCircleId()]=$circle;
            }                        
        }
        return circles;
    }   
    public function getCurrentUserCircles()
    {
        return $this->getUserCircles($this->Id);
    }
    public function getUserServiceTeams($userId)
    {
        $serviceTeams = array();
        $circles = $this->getCurrentUserCircles();
        foreach($circles as $circle)
        {
            $rows = $this->db->createCommand()
                        ->select()
                        ->from($this->serviceTeamTable)
                        ->where('circleid=:circleid', array(':circleid' => $circle->getCircleId()))
                        ->queryAll();
            foreach ($rows as $row) {
                $serviceTeams[$row['_id']]=new ServiceTeam($row['_id']);
                $Services[$row['serviceid']]=new Service($row['serviceid']);
            }
        }

        if(isset($serviceTeams))
        {
             foreach($serviceTeams as $servicTeam)
            {
                if($serviceTeam->ServiceTeamType == $ServiceTeam->WorkingTeam)
                {
                    $WorkingTeams[$serviceTeam->Id]=$serviceTeam;
                }
                if($serviceTeam->ServiceTeamType == $ServiceTeam->ServedTeam)
                {
                    $ServedTeams[$serviceTeam->Id]=$serviceTeam;
                }
                if($serviceTeam->ServiceTeamType == $ServiceTeam->FriendTeam)
                {
                    $FriendTeams[$serviceTeam->Id]=$serviceTeam;
                }
            }
        }
       // return $serviceTeams;
    }
    public function getCurrentUserServiceTeams()    
    {
        return $this->getUserServiceTeams($this->Id);
    }
    /*
    public function getUserServices($userId)
    {
        $this->getUserServiceTeams($userId);
        return $Services;
    }
     */
   

    
   


    


}